
var wechatService = require('../../services/weixin/wechatService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');

function serviceMessage(req,res,fnName,err,result,code){
    //console.log(result);
    code = code == null ? 1000: code;
    if (err) {
        logger.error('Failed to ' + fnName + '...!' + err.message);
        res.json(messageUtil.composeFailedMessage(req, err.message, code));
    } else {
        res.json(messageUtil.composeSucceededMessage(req, null, result, code));
    }
}

//exports.accessToken = function (req, res) {
//    var input = req.body;
//    wechatService.accessTokenService(input, function (err, result) {
//        serviceMessage(req,res,'accessTokenService',err,result);
//    })
//};
//
//exports.jsapiTicket = function (req, res) {
//    var input = req.body;
//    wechatService.jsapiTicketService(input, function (err, result) {
//        serviceMessage(req,res,'jsapiTicketService',err,result);
//    })
//};

exports.openId = function (req, res) {
    var code = req.body.code;
    wechatService.openIdService(code, function (err, result) {
        serviceMessage(req,res,'openIdService',err,result);
    })
};

exports.getWxConfig = function (req, res) {
    var url = req.body.url;
    wechatService.getWxConfigService(url, function (err, result) {
        serviceMessage(req,res,'getWxConfigService',err,result);
    })
};

exports.userProfile = function (req, res) {
    var openId = req.body.openId;
    wechatService.userProfileService(openId, function (err, result) {
        serviceMessage(req,res,'userProfileService',err,result);
    })
};

exports.getSubscribedUsers = function (req, res) {
    var query = req.query;
    wechatService.getSubscribedUsersService(query, function (err, result) {
        serviceMessage(req,res,'getSubscribedUsersService',err,result);
    })
};